package com.eazy.lkv2.web.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.entity.Role;
import com.eazy.lkv2.web.entity.RolePermission;
import com.eazy.lkv2.web.service.system.SystemMenuService;
import com.eazy.lkv2.web.service.system.SystemPermissionService;
import com.eazy.lkv2.web.service.system.SystemRolePermissionService;
import com.eazy.lkv2.web.service.system.SystemRoleService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @date 2016/1/28
 * @author jzx 
 * @desc 角色管理
 */
@Controller  
@RequestMapping("/role") 
public class SystemRoleController extends BaseController {

	@Autowired
	private SystemRoleService roleService;
	@Autowired
	private SystemRolePermissionService rolePermissionService;
	@Autowired
	private SystemMenuService systemMenuService;
	@Autowired
	private SystemPermissionService permissionService;
	
	/**
     * 查询用角色列表
     */
    @RequestMapping(value = "list" , method = RequestMethod.GET)  
    public ModelAndView selectRole() {
    	List<Role> data = roleService.queryByList(null);
    	addObject("data", data);
    	return getSuccess("admin/role_list");
    }
    
    /**
     * 修改角色
     */
    @RequestMapping(value = "updateRole" , method = RequestMethod.POST)
    public void updateRole(Role role) {
    	role.setRoleCode(role.getName());
    	role.setId(getParaToInt("id"));
    	roleService.update(role);
    	
    	roleService.deleteRolePermission(role.getId().toString());
    	
    	String[] check = getParaValues("check"); // 获取选中的菜单 
    	
    	List<RolePermission> batch = Lists.newArrayList();
    	
    	for(int i =0 ; i< check.length; i++) {
    		batch.add(new RolePermission(role.getId(),Integer.valueOf(check[i])));
    	}
    	rolePermissionService.createOfBatch(batch);
    	renderJson200();
    }
    
    /**
     * 跳转到修改角色页面
     */
    @RequestMapping(value = "toUpdateRole/{id}" , method = RequestMethod.GET)
    public ModelAndView toUpdateRole(@PathVariable String id) {
    	Map<String,Object> maps = Maps.newHashMap();
    	// 为了进行更流畅的体验，把菜单这部分缓存起来
    	
    	List<Map<String,Object>> check = roleService.getRolePermissions(id);
    	
     	// 1)获取所有菜单
    	List<Map<String,Object>> allMenu = systemMenuService.select();
    	// 2)获取所有子菜单
    	for (Map<String, Object> a : allMenu) {
    		for (Map<String, Object> c : check) {
    			if(String.valueOf(a.get("ID")).equals(String.valueOf(c.get("ID")))) {
    				a.put("menu", true);
    			}
    		}
    		List<Map<String,Object>> subMenu = systemMenuService.selectSubMenu(a.get("ID").toString());
    		
    		// 3)获取所有功能
    		for(Map<String,Object> b : subMenu) {
    			for (Map<String, Object> c : check) {
        			if(String.valueOf(b.get("ID")).equals(String.valueOf(c.get("ID")))) {
        				b.put("submenu", true);
        			}
        		}
    			List<Map<String,Object>> funMenu = systemMenuService.selectSubMenu(b.get("ID").toString());
    			for(Map<String,Object> f : funMenu) {
        			for (Map<String, Object> c : check) {
            			if(String.valueOf(f.get("ID")).equals(String.valueOf(c.get("ID")))) {
            				f.put("funmenu", true);
            			}
            		}
    			}
    			b.put("funMenu", funMenu);
    		}
    		a.put("subMenu", subMenu);
		}
    	maps.put("role", roleService.findById(id));
    	setAttr("check", check);
    	setAttr("allMenu", allMenu);
    	return new ModelAndView("admin/role_update",maps);
    }
}
